package com.cambright.stack.components
{
	import com.cambright.lotusroot.Component;
	
	import flash.display.DisplayObject;
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.NativeWindowRenderMode;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.NativeWindowType;
	import flash.display.Screen;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	
	public class WindowComponent extends Component
	{
		private var _window:NativeWindow;
		private var _target:DisplayObject;
		public function get target():DisplayObject
		{
			return _target;
		}
		public function set target(value:DisplayObject):void
		{
			_target = value;
		}
		
		private var _rect:Rectangle = new Rectangle(Number.MIN_VALUE, Number.MIN_VALUE, 200, 150);
		public function get rect():Rectangle
		{
			return _rect;
		}
		public function set rect(value:Rectangle):void
		{
			_rect = value;
		}
		
		private var _shadow:GlowFilter = new GlowFilter(0x333333, .9, 12, 12, 1, 3);
		public function get shadow():GlowFilter
		{
			return _shadow;
		}
		public function set shadow(value:GlowFilter):void
		{
			_shadow = value;
			_shadow.blurX = int(_shadow.blurX);
			_shadow.blurY = int(_shadow.blurY);
		}
		
		override protected function invoke():void
		{
			super.invoke();
			
			var options:NativeWindowInitOptions = new NativeWindowInitOptions();
			options.type = NativeWindowType.LIGHTWEIGHT;
			options.systemChrome = NativeWindowSystemChrome.NONE;
			options.resizable = false;
			options.maximizable = false;
			options.minimizable = false;
			options.renderMode = NativeWindowRenderMode.DIRECT;
			options.transparent = true;
			
			_window = new NativeWindow(options);
			_window.stage.align = StageAlign.TOP_LEFT;
			_window.stage.scaleMode = StageScaleMode.NO_SCALE;
			_window.stage.frameRate = 30;
		}
		
		override protected function enable():void
		{
			_window.stage.addChild(_target);
			_window.stage.addEventListener(Event.ENTER_FRAME, _onStageEnterFrame);
			_target.x = _shadow.blurX;
			_target.y = _shadow.blurY;
			_target.width = _rect.width;
			_target.height = _rect.height;
			_target.filters = [_shadow];
			var screenRect:Rectangle = Screen.mainScreen.visibleBounds;
			var x:int = _rect.x == Number.MIN_VALUE ? (screenRect.width - _rect.width) * .5 : _rect.x;
			var y:int = _rect.y == Number.MIN_VALUE ? (screenRect.height - _rect.height) * .5 : _rect.y;
			_window.x = x - _shadow.blurX;
			_window.y = y - _shadow.blurY;
			_window.width = _rect.width + _shadow.blurX * 2;
			_window.height = _rect.height + _shadow.blurY * 2;
			_window.activate();
		}
		
		public function activate():void
		{
			_window.activate();
		}
		
		override protected function disable():void
		{
			_window.visible = false;
			_target.filters = [];
			_window.stage.removeEventListener(Event.ENTER_FRAME, _onStageEnterFrame);
			_window.stage.removeChild(_target);
		}
		
		private function _move(event:Event):void
		{
			_window.startMove();
		}
		
		private function _onStageEnterFrame(event:Event):void
		{
			_window.stage.invalidate();
		}
		
		public function startMove():void
		{
			_window.startMove();
		}
		
		/*
		public function addTitleElement(element:TitleLayoutElement):void
		{
			_panel.addChild(element.display);
			_panel.addTitleElement(element);
		}
		*/
	}
}